//
//  TTCommonViewCell.m
//  TaoTuo
//
//  Created by admin on 2019/9/26.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTCommonViewCell.h"
#import "TTCellLabelModel.h"
#import "TTCellLabelArrowModel.h"
#import "TTCellLabelMiddleModel.h"
#import "TTCellArrowModel.h"
#import "TTCellImageArrowModel.h"
#import "TTCellSwitchModel.h"
#import "TTCellLabelRightButtonModel.h"
#import <UIImageView+WebCache.h>

@interface TTCommonViewCell ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *midLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UIImageView *arrowImageView;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIImageView *rightImageView;
@property (nonatomic, strong) UISwitch *mySwith;
@property (nonatomic, strong) UIButton *rightBtn;

@end

@implementation TTCommonViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor whiteColor];
        [self lineView];
        
    }
    return self;
}

- (void)mySwitchClick:(UISwitch *)sender
{
    TTCellSwitchModel *switchModel = (TTCellSwitchModel *)self.model;
    [USER_DEFAULT setBool:sender.on forKey:switchModel.key];
    [USER_DEFAULT synchronize];
    if (switchModel.changeBlock) {
        switchModel.changeBlock(sender.on);
    }
}


- (void)setModel:(TTCellModel *)model
{
    _model = model;
    self.titleLabel.text = model.title;
    self.contentLabel.text = model.content;
    self.midLabel.text = model.midText;

    if ([model isKindOfClass:[TTCellLabelModel class]]) {
        self.arrowImageView.hidden = YES;
        self.mySwith.hidden = YES;
        TTCellLabelModel *labelModel = (TTCellLabelModel *)model;
        self.contentLabel.textColor = labelModel.contentColor;
        [self.contentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView).mas_offset(-10);
            make.top.height.equalTo(self.contentView);
        }];
        
    } else if ([model isKindOfClass:[TTCellLabelMiddleModel class]]) {
        TTCellLabelMiddleModel *midModel = (TTCellLabelMiddleModel *)model;
        self.arrowImageView.hidden = YES;
        self.mySwith.hidden = YES;
        self.rightBtn.hidden = YES;
        if (midModel.midTextColor) {
            self.midLabel.textColor = midModel.midTextColor;
        }
        if (midModel.midFontSize) {
            self.midLabel.font = [UIFont systemFontOfSize:midModel.midFontSize];
        }
        
    } else if ([model isKindOfClass:[TTCellLabelArrowModel class]]) {
        self.arrowImageView.hidden = NO;
        self.mySwith.hidden = YES;
        self.rightBtn.hidden = YES;
        TTCellLabelArrowModel *labelArrowModel = (TTCellLabelArrowModel *)model;
        self.contentLabel.textColor = labelArrowModel.contentColor;
        [self.contentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.arrowImageView.mas_left).mas_offset(-5);
            make.top.height.equalTo(self.contentView);
        }];
    } else if ([model isKindOfClass:[TTCellArrowModel class]]) {
        self.arrowImageView.hidden = NO;
        self.mySwith.hidden = YES;
        self.rightBtn.hidden = YES;
    } else if ([model isKindOfClass:[TTCellImageArrowModel class]]) {
        self.arrowImageView.hidden = NO;
        self.mySwith.hidden = YES;
        self.rightBtn.hidden = YES;
        self.rightImageView.layer.cornerRadius = 20;
        TTCellImageArrowModel *imageArrowModel = (TTCellImageArrowModel *)model;
        if ([imageArrowModel.rightImageName containsString:@"http"]) {
            /// 是个链接
            self.rightImageView.contentMode = UIViewContentModeScaleAspectFill;
            [self.rightImageView sd_setImageWithURL:[NSURL URLWithString:imageArrowModel.rightImageName] placeholderImage:[UIImage imageNamed:@"header_img"] options:SDWebImageRetryFailed];
        } else {
            UIImage *image = [UIImage imageNamed:imageArrowModel.rightImageName];
            self.rightImageView.image = image;
            if (image.size.width < 40) {
                self.rightImageView.layer.cornerRadius = 0;
                self.rightImageView.contentMode = UIViewContentModeRight;
            } else {
                self.rightImageView.contentMode = UIViewContentModeScaleAspectFill;
            }
        }
        
        
    } else if ([model isKindOfClass:[TTCellSwitchModel class]]) {
        self.mySwith.hidden = NO;
        self.arrowImageView.hidden = YES;
        self.rightBtn.hidden = YES;
        TTCellSwitchModel *switchModel = (TTCellSwitchModel *)model;
        self.mySwith.on = [USER_DEFAULT boolForKey:switchModel.key];
        
    } else if ([model isKindOfClass:[TTCellLabelRightButtonModel class]]) {
        TTCellLabelRightButtonModel *rbModel = (TTCellLabelRightButtonModel *)model;
        [self.rightBtn setTitle:rbModel.rightButtonTitle forState:UIControlStateNormal];
        [self.rightBtn setTitleColor:rbModel.rightButtonTitleColor forState:UIControlStateNormal];
        [self.rightBtn addTarget:rbModel.target action:rbModel.selector forControlEvents:UIControlEventTouchUpInside];
        self.rightBtn.hidden = NO;
        self.arrowImageView.hidden = YES;
        self.mySwith.hidden = YES;
        
        
    } else {
        self.arrowImageView.hidden = YES;
        self.mySwith.hidden = YES;
        self.rightBtn.hidden = YES;
    }
    
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [UILabel labelWithTitle:@"" color:UIColorHex(333333) fontSize:15];
        [self.contentView addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.contentView);
            make.left.mas_equalTo(10);
            make.width.mas_greaterThanOrEqualTo(40);
            make.bottom.equalTo(self.contentView).mas_offset(-12.5);
        }];
    }
    return _titleLabel;
}

- (UILabel *)midLabel
{
    if (!_midLabel) {
        _midLabel = [UILabel labelWithTitle:@"" color:UIColorHex(333333) fontSize:15];
        _midLabel.numberOfLines = 0;
        [self.contentView addSubview:_midLabel];
        [_midLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.titleLabel.mas_right).mas_offset(3);
            make.right.equalTo(self.contentView).mas_offset(-50);
            make.bottom.equalTo(self.titleLabel);
        }];
    }
    return _midLabel;
}

- (UILabel *)contentLabel
{
    if (!_contentLabel) {
        _contentLabel = [UILabel labelWithTitle:@"" color:UIColorHex(FF1F1F) fontSize:15];
        [self.contentView addSubview:_contentLabel];
        [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.arrowImageView.mas_left).mas_offset(-5);
            make.top.height.equalTo(self.contentView);
        }];
        
    }
    return _contentLabel;
}

- (UIImageView *)arrowImageView
{
    if (!_arrowImageView) {
        UIImage *image = [UIImage imageNamed:@"btn_right_arrow"];
        _arrowImageView = [[UIImageView alloc] initWithImage:image];
        _arrowImageView.contentMode = UIViewContentModeCenter;
        [self.contentView addSubview:_arrowImageView];
        [_arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView).mas_offset(-10);
            make.width.mas_equalTo(image.size.width);
            make.top.height.equalTo(self.contentView);
        }];
    }
    return _arrowImageView;
}

- (UIImageView *)rightImageView
{
    if (!_rightImageView) {
        _rightImageView = [[UIImageView alloc] init];
        _rightImageView.contentMode = UIViewContentModeCenter;
        _rightImageView.layer.cornerRadius = 20;
        _rightImageView.layer.masksToBounds = YES;
        [self.contentView addSubview:_rightImageView];
        [_rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.arrowImageView.mas_left).mas_offset(-3);
            make.width.height.mas_equalTo(40);
            make.centerY.equalTo(self.contentView);
        }];
    }
    return _rightImageView;
}

- (UIButton *)rightBtn
{
    if (!_rightBtn) {
        _rightBtn = [UIButton createButtonWithTitle:@"" titleColor:kMainColor titleFontSize:12 borderColor:kMainColor cornerRadius:10 contentEdgeInsets:UIEdgeInsetsZero target:nil action:nil];
        [self.contentView addSubview:_rightBtn];
        [_rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView).mas_offset(-10);
            make.centerY.equalTo(self.contentView);
            make.width.mas_equalTo(40);
            make.height.mas_equalTo(20);
        }];
    }
    return _rightBtn;
}

- (UISwitch *)mySwith
{
    if (!_mySwith) {
        _mySwith = [[UISwitch alloc] init];
        _mySwith.onTintColor = kMainColor;
        _mySwith.transform = CGAffineTransformMakeScale(0.75, 0.75);
        [_mySwith addTarget:self action:@selector(mySwitchClick:) forControlEvents:UIControlEventValueChanged];
        [self.contentView addSubview:_mySwith];
        [_mySwith mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.contentView);
            make.right.equalTo(self.contentView).mas_offset(-10);
        }];
    }
    return _mySwith;
}

- (UIView *)lineView
{
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = kSeparatorColor;
        [self.contentView addSubview:_lineView];
        [_lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(10);
            make.right.equalTo(self.contentView).mas_offset(-10);
            make.height.mas_equalTo(1);
            make.bottom.equalTo(self.contentView);
        }];
    }
    return _lineView;
}

@end
